package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.dao.TopImgDao;
import com.soft.front.dto.TopImgDTO;
import com.soft.model.TopImg;

@SuppressWarnings("unchecked")
@Component
public class TopImgDaoImpl extends BaseDaoImpl implements TopImgDao {

	@CacheEvict(value = "TOPIMG_INFO_CACHE", allEntries = true)
	public void addOrEdit(TopImg topImg) {
		this.saveOrUpdate(topImg);
	}
	
	@CacheEvict(value = "TOPIMG_INFO_CACHE", allEntries = true)
	public void remove(TopImg topImg) {
		this.delete(topImg);
	}

	@Cacheable(value = "TOPIMG_INFO_CACHE")
	public Map<String, Object> getLst() {
		Map<String, Object> resMap = new HashMap<String, Object>();
		//顶部图片
		List<TopImg> topImgs = this.findAll(TopImg.class, new Order[]{Order.desc("level"), Order.desc("id")}, 
				Restrictions.eq("state", 1));
		List<TopImgDTO> dtoTopImgs = new ArrayList<TopImgDTO>();
		for (TopImg topImg : topImgs) {
			TopImgDTO dto = new TopImgDTO();
			BeanUtils.copyProperties(topImg, dto);
			dtoTopImgs.add(dto);
		}
		resMap.put("topImgs", dtoTopImgs);
		return resMap;
	}

}
